package study.ct.com.rxjavaapplication.MainFragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;

import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.List;
import java.util.zip.Inflater;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import study.ct.com.rxjavaapplication.MyApplication;
import study.ct.com.rxjavaapplication.R;
import study.ct.com.rxjavaapplication.bin.TripRes;
import study.ct.com.rxjavaapplication.service.CtService;
import study.ct.com.rxjavaapplication.util.ILog;

/**
 * Created by ct on 2017/9/29 0029.
 */

public class FragmentTrip extends Fragment {

    private static final String TAG = "FragmentTrip";
    private View view;
    private RecyclerView recycle_view;
    private List<TripRes.Result.Values> listData=new ArrayList<>();
    private TripAdapter tripAdapter;
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ILog.i(TAG, "onCreate: initData");
        initData();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        ILog.i(TAG, "onCreateView: ");
        view=inflater.inflate(R.layout.fragment_trip,container,false);
        recycle_view = view.findViewById(R.id.recycle_view);
        return view ;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        ILog.i(TAG, "onActivityCreated: ");
        tripAdapter = new TripAdapter(listData);
        recycle_view.setLayoutManager(new LinearLayoutManager(getContext()));
        recycle_view.setAdapter(tripAdapter);
    }

    @Override
    public void onStart() {
        super.onStart();
        Log.i(TAG, "onStart: ");
    }

    public void  initData(){

        //添加 log拦截
        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor(
                new HttpLoggingInterceptor.Logger(){

                    @Override
                    public void log(String message) {
                        Log.i(TAG, "log: Retrofit=>"+message);
                    }
                });

        loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

        OkHttpClient client = new OkHttpClient.Builder()
                .addInterceptor(loggingInterceptor)
                .build();

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(MyApplication.BaseUrl)
                .addConverterFactory(GsonConverterFactory.create())
                .client(client)
                .build();

        CtService service = retrofit.create(CtService.class);



        final Call<TripRes> call= service.getTrip("");

        new Thread(new Runnable() {
            @Override
            public void run() {
                call.enqueue(new Callback<TripRes>() {
                    @Override
                    public void onResponse(Call<TripRes> call, Response<TripRes> response) {
                        Log.i(TAG, "onResponse: Thread="+Thread.currentThread().getName());
                        listData.addAll(response.body().getResult().getValue());
                        tripAdapter.notifyDataSetChanged();
                        Log.i(TAG, "onResponse: ==listData=="+listData.size() );

                    }

                    @Override
                    public void onFailure(Call<TripRes> call, Throwable t) {
                        Log.i(TAG, "onFailure: "+t);
                        Toast.makeText(getContext(), " 初始化数据失败", Toast.LENGTH_LONG);
                    }
                });
            }
        }).start();

    }

    public class TripAdapter extends RecyclerView.Adapter<TripAdapter.TripHolder>{

        private List<TripRes.Result.Values> listValues;
        public TripAdapter(List<TripRes.Result.Values> listValues){
            this.listValues=listValues;
        }

        @Override
        public TripHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.trip_item, parent, false);
            TripHolder holder = new TripHolder(view);
            return holder;
        }

        @Override
        public void onBindViewHolder(TripHolder holder, int position) {
            holder.address.setText(listValues.get(position).getAddress());
            holder.areaName.setText(listValues.get(position).getAreaName());
            holder.introduce.setText(listValues.get(position).getIntroduce());
            Glide.with(getContext())
                    .load(listValues.get(position).getBigImg())
                    .into(holder.image);
            holder.price.setText(listValues.get(position).getPriceDesc());
            holder.name.setText(listValues.get(position).getName());

        }

        @Override
        public int getItemCount() {
            //Log.i(TAG, "getItemCount: =="+listValues.size());
            return listValues.size();
        }

        class TripHolder extends RecyclerView.ViewHolder {
            TextView address;
            TextView areaName;
            ImageView image;
            TextView introduce;
            TextView price;
            TextView name;


            public TripHolder(View itemView) {
                super(itemView);
                name = itemView.findViewById(R.id.name);
                address = itemView.findViewById(R.id.address);
                areaName = itemView.findViewById(R.id.area_name);
                image = itemView.findViewById(R.id.image);
                introduce = itemView.findViewById(R.id.introduce);
                price = itemView.findViewById(R.id.price_desc);
            }
        }
    }


}
